package org.gesplan.util;

import com.google.gdata.client.calendar.*;
import com.google.gdata.data.*;
import com.google.gdata.data.calendar.*;
import com.google.gdata.data.extensions.*;
import com.google.gdata.util.*;
import java.io.*;
import java.net.*;
import java.util.logging.Logger;


public class CalendarManager {

    private static final String OWN = "http://www.google.com/calendar/feeds/default/owncalendars/full";
    private static CalendarService myService;
    private static CalendarFeed calendarFeed;
    private static URL feedUrl;
    private static URL calendarUrl;
    private static Logger logger;    

    
    public static void initCalendar(){
        try {
            logger = Utils.getLogger();
            myService = new CalendarService("Rosersa-Calendari");
            myService.setUserCredentials("calendari.rosersa@gmail.com", "roses2012");
            feedUrl = new URL(OWN);
            calendarFeed = myService.getFeed(feedUrl, CalendarFeed.class);
        } catch (ServiceException | IOException ex) {
            logger.severe(ex.getMessage());
        }
    }
    
    public static void updateCalendar(String name) {
    }


    public static void createEvent(int calendar, String title, String content, String startDate, String endDate) {

        CalendarEntry calendarEntry = calendarFeed.getEntries().get(calendar);
        // Afegint la part del getlink agafem la url que toca per fer el POST al calendari que volem    
        String url = calendarEntry.getLink("alternate", "application/atom+xml").getHref();
        try {        
            calendarUrl = new URL(url);
        } catch (MalformedURLException ex) {
            System.out.println(ex.getMessage());
        }
        CalendarEventEntry myEntry = new CalendarEventEntry();

        myEntry.setId("aa");
        myEntry.setTitle(new PlainTextConstruct(title));
        myEntry.setContent(new PlainTextConstruct(content));
        //myEntry.setColor(new ColorProperty("#88880E"));
        DateTime startTime = DateTime.parseDate(startDate);
        DateTime endTime = DateTime.parseDate(endDate);
        When eventTimes = new When();
        eventTimes.setStartTime(startTime);
        eventTimes.setEndTime(endTime);
        myEntry.addTime(eventTimes);
        try {
            // Send the request and receive the response:
            CalendarEventEntry insertedEntry = myService.insert(calendarUrl, myEntry);
            if (insertedEntry == null){
                logger.info("Error a l'insertar event a Google Calendar");
            }
            System.out.println(myEntry.getId());
        } catch (IOException | ServiceException ex) {
            logger.severe(ex.getMessage());
        }

    }


    public static void editEvent() {

        CalendarEventFeed myFeed;
        try {
            myFeed = myService.getFeed(calendarUrl, CalendarEventFeed.class);
            for (int i = 0; i < myFeed.getEntries().size(); i++) {
                CalendarEventEntry retrievedEntry = myFeed.getEntries().get(i);
                if (retrievedEntry.getId().equals("aa")) {
                    System.out.println("Event");
                }
                System.out.println("\t" + retrievedEntry.getId());
                System.out.println("\t" + retrievedEntry.getTitle().getPlainText());
            }
            CalendarEventEntry retrievedEntry = myFeed.getEntries().get(0);
            retrievedEntry.setTitle(new PlainTextConstruct("Imp"));
            URL editUrl = new URL(retrievedEntry.getEditLink().getHref());
            System.out.println(editUrl);
            CalendarEventEntry updatedEntry = (CalendarEventEntry) myService.update(editUrl, retrievedEntry);
            if (updatedEntry == null){
                logger.info("Error a l'actualizar event a Google Calendar");
            }            
        } catch (IOException | ServiceException ex) {
            logger.severe(ex.getMessage());
        }

    }
    
    
    public static void retrieveCalendars(){
        System.out.println("Your calendars:\n");
        for (int i = 0; i < calendarFeed.getEntries().size(); i++) {
            CalendarEntry entry = calendarFeed.getEntries().get(i);
            System.out.println("\t" + entry.getTitle().getPlainText());
        }             
    }   
    
    
    public static void retrieveEvents(){
        try {
            CalendarEventFeed myFeed = myService.getFeed(calendarUrl, CalendarEventFeed.class);
            for (int i = 0; i < myFeed.getEntries().size(); i++) {
                CalendarEventEntry retrievedEntry = myFeed.getEntries().get(i);
                if (retrievedEntry.getId().equals("aa")) {
                    System.out.println("Find entry");
                }
                System.out.println("\t" + retrievedEntry.getId());
                System.out.println("\t" + retrievedEntry.getTitle().getPlainText());
            }              
        } catch (IOException | ServiceException ex) {
            logger.severe(ex.getMessage());
        }
   }
    
    
}